const CategoryModel = require("../modules/category");
 
class CategoryController {
    /**
     * 创建学生信息
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async create(ctx){
        //接收客服端
        let req = ctx.request.body;
        if(req.name){
            try{
                let categoryData=await CategoryModel.getCategoryDetail({name:req.name});
                if(categoryData&&categoryData.name==req.name){
                    ctx.response.status = 201;
                    ctx.body = {
                        code: 201,
                        msg: '该菜单名已存在，请勿重复添加'
                    }
                    return
                }
                //创建模型
                const ret = await CategoryModel.createCategory(req);
                //使用刚刚创建ID查询详情，且返回详情信息
                // const data = await CategoryModel.getCategoryDetail(ret.id);
 
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '创建成功',
                }
            }catch(err){
                ctx.response.status = 201;
                ctx.body = {
                    code: 201,
                    msg: '创建失败',
                    data: err
                }
            }
        }else {
            ctx.response.status = 201;
            ctx.body = {
                code: 201,
                msg: '参数不齐全'
            }
        }
    }
 
    /**
     * 获取文章详情
     * @param ctx
     * @returns {Promise.<void>}
     */
    static async detail(ctx){
        let id = ctx.params.id;
        if(id){
            try{
                // 查询菜单信息详情模型
                let data = await CategoryModel.getCategoryDetail(id);
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '查询成功',
                    data
                }
            }catch(err){
                ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '查询失败',
                    data
                }
            }
        }else {
            ctx.response.status = 416;
            ctx.body = {
                code: 416,
                msg: 'ID必须传'
            }
        }
    }

    /**查询全部菜单
     * @param {}
     * @return {Promise.<void>}
     */
    static async categoryList(ctx){
        try {
            let data= await CategoryModel.getCategory();
            ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '查询成功',
                    data
                }
        } catch (error) {
            ctx.response.status = 412;
                ctx.body = {
                    code: 412,
                    msg: '查询失败',
                    data
                }
        }
    }

    /**
     * 根据id删除菜单
     */
    static async delete(ctx){
        let id=ctx.params.id;
        if(id){
            try {
                let data= await CategoryModel.deleteCategory(id);
                if(data===0)throw ''
                ctx.response.status = 200;
                ctx.body = {
                    code: 200,
                    msg: '删除成功',
                    data
                }
            } catch (error) {
                ctx.response.status = 201;
                    ctx.body = {
                        code: 400,
                        msg: `删除失败 ${error}`
                    } 
            }
        }else {
            ctx.response.status = 201;
            ctx.body = {
                code: 201,
                msg: 'ID必须传'
            } 
        }
    }

}
 
module.exports = CategoryController;